<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java字符串处理：String、StringBuffer与StringBuilder深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4a5568;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 代码样式 */
        .code-inline {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.125rem 0.5rem;
            border-radius: 0.375rem;
            font-family: 'Courier New', monospace;
            font-weight: 500;
            display: inline-block;
            transform: translateY(-1px);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        /* 卡片悬停效果 */
        .feature-card {
            transition: all 0.3s ease;
            background: white;
            border-radius: 1rem;
            overflow: hidden;
        }
        
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        /* 表格样式 */
        .comparison-table {
            background: white;
            border-radius: 1rem;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .comparison-table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
        }
        
        .comparison-table tr:nth-child(even) {
            background: #f7fafc;
        }
        
        /* 动画效果 */
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        
        /* 图标装饰 */
        .section-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            margin-bottom: 1rem;
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        /* Mermaid图表样式 */
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="relative overflow-hidden">
        <div class="absolute inset-0 bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 opacity-90"></div>
        <div class="relative container mx-auto px-6 py-24 text-center text-white">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                Java字符串处理的艺术
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 animate-fadeInUp" style="animation-delay: 0.2s;">
                深入理解 String、StringBuffer 与 StringBuilder 的本质区别
            </p>
            <div class="flex justify-center space-x-8 animate-fadeInUp" style="animation-delay: 0.4s;">
                <div class="text-center">
                    <i class="fas fa-lock text-3xl mb-2"></i>
                    <p class="text-sm">不可变性</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync-alt text-3xl mb-2"></i>
                    <p class="text-sm">线程安全</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-tachometer-alt text-3xl mb-2"></i>
                    <p class="text-sm">性能优化</p>
                </div>
            </div>
        </div>
    </section>

    <!-- 主要内容 -->
    <main class="container mx-auto px-6 py-12">
        <!-- 引言 -->
        <section class="max-w-4xl mx-auto mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    在Java编程世界中，字符串处理是最基础也是最重要的技能之一。<span class="code-inline">String</span>、<span class="code-inline">StringBuffer</span> 和 <span class="code-inline">StringBuilder</span> 作为处理字符串的三大核心类，它们各自拥有独特的特性和适用场景。深入理解这三者的区别，不仅能帮助我们写出更高效的代码，更能让我们在面对不同的业务需求时做出最优的技术选择。
                </p>
            </div>
        </section>

        <!-- 概念关系图 -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">
                    <i class="fas fa-project-diagram mr-3 text-indigo-600"></i>
                    三者关系概览
                </h2>
                <div class="mermaid">
                    graph TD
                        A[Java字符串处理] --> B[String]
                        A --> C[StringBuffer]
                        A --> D[StringBuilder]
                        
                        B --> B1[不可变性]
                        B --> B2[字符串常量池]
                        B --> B3[线程安全]
                        
                        C --> C1[可变性]
                        C --> C2[synchronized同步]
                        C --> C3[线程安全]
                        
                        D --> D1[可变性]
                        D --> D2[无同步机制]
                        D --> D3[非线程安全]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#38b2ac,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- String详解 -->
        <section class="mb-16">
            <div class="feature-card p-8 md:p-12">
                <div class="section-icon">
                    <i class="fas fa-lock"></i>
                </div>
                <h2 class="text-3xl font-bold mb-6 text-gray-800">
                    1. String - 不可变的经典
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    <span class="code-inline">String</span> 是Java中最常用的类之一，它的核心特性是<strong>不可变性（Immutability）</strong>。这意味着一旦String对象被创建，其内容就无法被修改。每次对String的"修改"操作，实际上都会创建一个全新的String对象。
                </p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-gradient-to-br from-green-50 to-green-100 p-6 rounded-xl">
                        <h3 class="text-xl font-semibold mb-3 text-green-800">
                            <i class="fas fa-check-circle mr-2"></i>优势特点
                        </h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-angle-right mr-2 text-green-600"></i>线程安全：不可变性天然保证了线程安全</li>
                            <li><i class="fas fa-angle-right mr-2 text-green-600"></i>字符串常量池：相同字符串共享内存，节省空间</li>
                            <li><i class="fas fa-angle-right mr-2 text-green-600"></i>可作为HashMap的键：内容不变，哈希值稳定</li>
                            <li><i class="fas fa-angle-right mr-2 text-green-600"></i>安全性高：不会被意外修改</li>
                        </ul>
                    </div>
                    
                    <div class="bg-gradient-to-br from-red-50 to-red-100 p-6 rounded-xl">
                        <h3 class="text-xl font-semibold mb-3 text-red-800">
                            <i class="fas fa-exclamation-triangle mr-2"></i>使用限制
                        </h3>
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-angle-right mr-2 text-red-600"></i>频繁修改会产生大量临时对象</li>
                            <li><i class="fas fa-angle-right mr-2 text-red-600"></i>可能导致内存浪费和GC压力</li>
                            <li><i class="fas fa-angle-right mr-2 text-red-600"></i>大量拼接操作性能较差</li>
                        </ul>
                    </div>
                </div>

                <div class="bg-gray-50 p-6 rounded-xl">
                    <h4 class="text-lg font-semibold mb-3 text-gray-800">
                        <i class="fas fa-lightbulb mr-2 text-yellow-500"></i>最佳实践场景
                    </h4>
                    <p class="text-gray-700">
                        String适合用于：配置信息存储、作为Map的键、少量的字符串操作、需要在多个对象间共享的字符串常量等场景。
                    </p>
                </div>
            </div>
        </section>

        <!-- StringBuffer详解 -->
        <section class="mb-16">
            <div class="feature-card p-8 md:p-12">
                <div class="section-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
                    <i class="fas fa-shield-alt"></i>
                </div>
                <h2 class="text-3xl font-bold mb-6 text-gray-800">
                    2. StringBuffer - 线程安全的选择
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    <span class="code-inline">StringBuffer</span> 是可变的字符序列，它通过<strong>synchronized</strong>关键字实现了线程安全。这使得它成为多线程环境下处理字符串的理想选择。
                </p>
                
                <div class="bg-gradient-to-r from-purple-100 to-pink-100 p-8 rounded-xl mb-8">
                    <h3 class="text-2xl font-semibold mb-4 text-purple-800">
                        <i class="fas fa-cogs mr-2"></i>核心特性
                    </h3>
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="text-center">
                            <div class="text-4xl mb-3 text-purple-600">
                                <i class="fas fa-sync-alt"></i>
                            </div>
                            <h4 class="font-semibold mb-2">同步机制</h4>
                            <p class="text-sm text-gray-700">所有公共方法都使用synchronized修饰，确保线程安全</p>
                        </div>
                        <div class="text-center">
                            <div class="text-4xl mb-3 text-pink-600">
                                <i class="fas fa-expand-arrows-alt"></i>
                            </div>
                            <h4 class="font-semibold mb-2">动态扩容</h4>
                            <p class="text-sm text-gray-700">内部数组自动扩容，无需手动管理容量</p>
                        </div>
                        <div class="text-center">
                            <div class="text-4xl mb-3 text-purple-600">
                                <i class="fas fa-memory"></i>
                            </div>
                            <h4 class="font-semibold mb-2">内存效率</h4>
                            <p class="text-sm text-gray-700">避免创建大量临时对象，减少GC压力</p>
                        </div>
                    </div>
                </div>

                <div class="bg-gray-50 p-6 rounded-xl">
                